package com.vicene.gmail.product.web;

import com.vicene.gmail.product.entity.CategoryEntity;
import com.vicene.gmail.product.service.CategoryService;
import com.vicene.gmail.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {


    @Autowired
    RedissonClient redissonClient;


    @Autowired
    CategoryService categoryService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping({"/" , "/index.html"})
    public String indexPage(Model model){
        List<CategoryEntity> categoryEntityList = categoryService.getLevel1Categorys();

        model.addAttribute("categorys" , categoryEntityList);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map< String ,List<Catelog2Vo>> getCatalogJson(){
        Map< String ,List<Catelog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }


    //读写锁，写锁释放完，读锁才能读取，并发读的过程中不上锁
    //保证获取得永远都是最新的写锁值
    //https://github.com/redisson/redisson/wiki/目录
    /*
     *修改期间，写锁是排他锁，读锁是共享锁，写锁不释放，读锁一直等待
     */
    @ResponseBody
    @GetMapping("/readValue")
    public String readValue(){
        RReadWriteLock rReadWriteLock = redissonClient.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = rReadWriteLock.readLock();
        try{
            rLock.lock();
            s = stringRedisTemplate.opsForValue().get("writevalue" );
        }catch (Exception e){

        }finally {
            rLock.unlock();
        }
        return s;
    }

    //读写锁，写锁释放完，读锁才能读取，并发读的过程中不上锁
    @ResponseBody
    @GetMapping("/writeValue")
    public String writeValue(){
        RReadWriteLock rReadWriteLock = redissonClient.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = rReadWriteLock.writeLock();
        try{
            rLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(10000);
            stringRedisTemplate.opsForValue().set("writevalue" , s);
        }catch (Exception e){

        }finally {
            rLock.unlock();
        }
        return s;

    }

    @GetMapping("lockDoor")
    @ResponseBody
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();
        return "放假了？";
    }

    @GetMapping("/gogogo/{id}")
    @ResponseBody
    public String gogogo(@PathVariable("id") Long id) throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();

        return id + "班的人走了";
    }
    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore rSemaphore = redissonClient.getSemaphore("park");
        rSemaphore.acquire();
        return "ok";
    }

    @GetMapping("/go")
    @ResponseBody
    public String go(@PathVariable("id") Long id) throws InterruptedException {
        RSemaphore rSemaphore = redissonClient.getSemaphore("park");
        rSemaphore.release();

        return "ok";
    }

    @GetMapping("/success")
    public String success(String code) {
//        RSemaphore rSemaphore = redissonClient.getSemaphore("park");
//        rSemaphore.release();

        return "ok";
    }



}
